<?
ob_start();
include("/var/www/html/new/d/common/header.html");
include("/var/www/html/new/d/common/left.html");
include("/var/www/html/new/d/common/page.php");
include("/var/www/html/new/d/class/SimpleExcel.class.php");
$out_html=ob_get_contents();
ob_end_clean();
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
/*if($_GET['dtype']==7) //分销联盟分润
    include_once 'distributor_union_lirun.php';
*/
$dtypeT=array(0=>"购买产品",1=>"撤改订单",3=>"账户资金变化",4=>"供应商授信余额",5=>"利润",6=>"提现冻结",7=>"电子凭证费",8=>"短信息费",9=>"银行交易手续费",10=>"凭证费",11=>"供应商信用额度变化 ",12=>"取消提现",13=>"拒绝提现",14=>"退款手续费");
$ptypeT=array(-1=>"支付宝",0=>"账号资金",1=>"支付宝",2=>"供应商可用资金",3=>"供应授信额度设置",4=>'网银');
$r=array();
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
    $currentPage = 1;
    $pageSize = 9999999;
}
$where1=$where2=$where3="where fid=".$_SESSION['sid'];
$where1.=" and dmoney>0 ";
if($beginDate=mysql_real_escape_string($_REQUEST['beginDate'])){
    $where1.=" and rectime>='$beginDate 00:00:00'";
    $where2.=" and rectime>='$beginDate 00:00:00'";
    $where3.=" and dtime>='$beginDate 00:00:00'";
}
if($endDate=mysql_real_escape_string($_REQUEST['endDate'])){
    $where1.=" and rectime<='$endDate 23:59:59'";
    $where2.=" and rectime<='$endDate 23:59:59'";
    $where3.=" and dtime<='$endDate 23:59:59'";
}
if($_REQUEST['daction']==="0"||$_REQUEST['daction']==="1"){
    $daction=$_REQUEST['daction'];
    $where1.=" and daction=$daction";
    if($daction==1){
        $where2="where -1=1";
        $where3="where -1=1";
    }
}
if($_REQUEST['dtype']||$_REQUEST['dtype']==="0"){
    $where1.=" and dtype=".$_REQUEST['dtype'];
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="pft"){
    $where1.=" and ptype in (0,1)";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="credit"){
    $where1.=" and ptype=2";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="creditSet"){
    $where1.=" and ptype=3";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="alipay"){
    $where1="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="tenpay"){
    $where1.=" and ptype=4";
    $where1="where -1=1";
    $where3="where -1=1";
}
if($_GET['orderid']){
    $where1 .= " and orderid={$_GET['orderid']}";
    $where2 .= " and orderid={$_GET['orderid']}";
    $where3 .= " and out_trade_no={$_GET['orderid']}";
}
$dnames=$orders=array();
$str="select id,dname from pft_member where dtype=0";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
    $dnames[$row['id']]=$row['dname'];
}

if(isset($_REQUEST['account']) && $_REQUEST['account']=='ipsPay') {
    //TODO:IPS支付记录
    $where3 .= " AND status=1";
    $sql_ips_rec = "SELECT status,total_fee,out_trade_no as orderid,dtime as rectime ".
        "FROM pft_ips_rec {$where3}";
    $GLOBALS['le']->query($sql_ips_rec);
    while($row=$GLOBALS['le']->fetch_assoc()) {
        $row['pay']="<em class='red'>-".($row['total_fee']/100)."</em>";
        $row['dtypeT']="购买产品";
        $row['ptype']=4;
        $r[]=$row;
    }
    $sql_count = "SELECT count(*) as cnt FROM pft_ips_rec {$where3}";
    $GLOBALS['le']->query($sql_count);
    $GLOBALS['le']->fetch_assoc();
    $count = $GLOBALS['le']->f('cnt');
}
else {

//    $str1 = "select id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype"
//        .",ptype from pft_member_journal $where1 order by rectime desc,"
//        ." orderid desc limit $offset,$pageSize";
//    $GLOBALS['le']->query($str1);
//    $tmp1 = array();
//    $tmp2 = array();
//    while($r1 = $GLOBALS['le']->fetch_assoc()) {
//        $tmp1[] = $r1;
//    }
//    $str2 = "select id,fid,money,orderid,rectime,ali_account "
//        . " from pft_member_alipay $where2 order by rectime desc,orderid desc"
//        . " limit $offset,$pageSize";
//    $GLOBALS['le']->query($str2);
//    while($r2 = $GLOBALS['le']->fetch_assoc()) {
//        $tmp2[] = $r1;
//    }
    /*
    $str="(select id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype,ptype,''".
        " as ali_account from pft_member_journal $where1 ) UNION".
        " (select id,fid,money,'','',orderid,daction,rectime,dtype,-1,ali_account from ".
        "pft_member_alipay $where2 ) order by rectime desc,orderid desc limit $offset,$pageSize";
    */
    if($_REQUEST['account']=='alipay'){
        $str="select id,fid,money,money as dmoney ,'' as lmoney,'' as aid,orderid,daction,rectime,dtype,-1 as ptype,ali_account from pft_member_alipay $where2 order by rectime desc,orderid desc limit $offset,$pageSize";

    }else{
        $str="select opid,id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype,ptype,''".
            " as ali_account,memo from pft_member_journal $where1 order by rectime desc,orderid desc limit $offset,$pageSize ";
        if($_GET['zmf']){
            echo $str;
        }
    }
    // echo $str;

    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
        if($row['daction']==0){
            $row['income']="<em class='green'>+".($row['dmoney']/100).($row['aid']?"</br>(".$dnames[$row['aid']]."授信)</em>":"");
        }elseif($row['daction']==2){
            $row['pay']="<em style='color:#2288EE;'>-".($row['dmoney']/100)."</em>";
        }else{
            $row['pay']="<em class='red'>-".($row['dmoney']/100)."</em>";
        }
        if($row['dtype']==-1){
            $row['dtypeT']="支付宝账号".$row['ali_account'];
            $row['alipay']="账户+".($row['dmoney']/100);
            $row['alipay']="账户+".($row['dmoney']/100);
        } elseif ($row['dtype']==-2) {
            $row['dtypeT']="支付宝账号".$row['ali_account'];
            $row['alipay']="账户-".($row['dmoney']/100);
        }
        if($row['dtype']==6&&$row['daction']==0)
        {
            if($row['opid']==$row['fid'])
                $row['dtype']=12;
            if($row['opid']!=$row['fid'])
                $row['dtype']=13;
        }
        $r[]= $row;

    }print_r($r);

    if($_REQUEST['account']=='alipay'){
        $str="select count(*) as total from pft_member_alipay $where2";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        $count2=$GLOBALS['le']->f("total");
        $count1 = 0;
    }else{
        $str="select count(*) as total from pft_member_journal $where1";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        $count1=$GLOBALS['le']->f("total");
        $count2=0;
    }
    $count=$count1+$count2;
    if(!isset($_REQUEST['account'])) $_REQUEST['account'] = $_GET['account'] = 'pft';
}
/*
 * 处理账户余额显示错误的问题 原因是数据库同时对一条数据进行处理 2015-04-17 def
 * 以最后一个行的余额为基准 array_pop 对其他行的余额进行加减运算来得到正确的余额 不使用从数据库里查出来的值
 * 先倒序一次 方便foreach 再倒回来
*/
if(!$_GET['dtype'] && $_GET['account']=="pft" /* && !strpos('1<>1',$where1)*/){
    $last = array_pop($r);
    $last_money = $last['lmoney'];
    krsort($r);
    foreach($r as $k => $v){
        $last_money = $r[$k][$lmoney] = $v['daction'] == 1 ?
            $last_money - $v['dmoney']: $last_money + $v['dmoney'];
    }
    ksort($r);
    array_push($r,$last);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){

    $filename=date('YmdHis').'交易记录';
    $Excel[0]['rectime']='交易时间';
    $Excel[0]['orderid']='订单号';
    $Excel[0]['dtype']='交易内容';
    $Excel[0]['ptype']='交易方式';
    $Excel[0]['pay']='支出';
    $Excel[0]['income']='收入';
    $Excel[0]['alipay']='银行账户';
    $Excel[0]['lmoney']='账户余额';
    $Excel[0]['lmoney2']='授信余额';
    $i=1;
    foreach($r as $lid=>$row){
        //print_r($row);
        $n++;
        $Excel[$n]['rectime']=$row['rectime'];
        $Excel[$n]['orderid']=$row['orderid'];
        $Excel[$n]['dtype']=$dtypeT[$row['dtype']] ?$dtypeT[$row['dtype']]:$row['dtypeT'];
        $Excel[$n]['ptype']=$ptypeT[$row['ptype']];
//		$Excel[$n]['pay']=strip_tags($row['pay'])?strip_tags($row['pay']):strip_tags($row['income']);
        $Excel[$n]['pay'] = strip_tags($row['pay']);
        $Excel[$n]['income'] = strip_tags($row['income']);
        $Excel[$n]['alipay']=$row['alipay'];
        $Excel[$n]['lmoney']=($row['ptype']==0||$row['ptype']==1)?$row['lmoney']/100:"";
        $Excel[$n]['lmoney2']=($row['ptype']==2||$row['ptype']==3)?($row['lmoney']/100)."(".$dnames[$row['aid']].")":"";
    }
    // print_r($dtypeT);
    $xls = new SimpleExcel('UTF-8', true,'orderList');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
    exit;
}
echo $out_html;
include("/var/www/html/new/d/tpl/trecord.tpl");
include("/var/www/html/new/d/common/footer.html");
?>


